package com.easyschool.model.bo;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.easyschool.model.school.School;

public class BoSchool {
	private static BoSchool instance;
	private BoSchool(){}
	
	public static BoSchool getInstance(){
		if(instance == null) instance = new BoSchool();
		return instance;
	}
	
	
	private static SessionFactory sessionFactory = null;
	@SuppressWarnings("unchecked")
	public List<School> getAllSchools() throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				@SuppressWarnings("unchecked")
				List<School> list = (List<School>)session.createCriteria(School.class).list();
				
				
				
				tx.commit();
				
				return list;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public School getSchool(Integer schoolId) throws Exception{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				School school = (School)session.createCriteria(School.class).add(Restrictions.idEq(schoolId)).uniqueResult();
				
				tx.commit();
				
				return school;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public List<School> getAllSchoolsByFilters(String libelle, String adresse1, String adresse2, String adresse3, Integer codePostal, String ville) throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				Criteria crit = session.createCriteria(School.class);
				
				if(libelle != null){
					crit.add( Restrictions.like("libelle", "%"+libelle+"%") );
				}
				if(adresse1 != null){
					crit.add( Restrictions.like("adresse1", "%"+adresse1+"%") );
				}
				if(adresse2 != null){
					crit.add( Restrictions.like("adresse2", "%"+adresse2+"%") );
				}
				if(adresse3 != null){
					crit.add( Restrictions.like("adresse3", "%"+adresse3+"%") );
				}
				if(codePostal != null){
					crit.add(Restrictions.eq("codePostal", codePostal));
				}
				if(ville != null){
					crit.add( Restrictions.like("ville", "%"+ville+"%") );
				}
				
				
				@SuppressWarnings("unchecked")
				List<School> list = crit.list();
				
				tx.commit();
				
				return list;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public Integer addSchool(School school) throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				Integer id = (Integer)session.save(school);
				
				tx.commit();
				
				return id;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public Integer removeUser(Integer idEcole) throws Exception{
		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				session.delete(new School(idEcole));
				
				tx.commit();
				
				return idEcole;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	
	}
}
